<?php

namespace app\admin\model;

use app\admin\controller\user\Agency;
use think\Model;


class Order extends Model
{

    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_callback_text',
        'order_status_text',
        'is_recharged_text',
        'callback_time_text',
        'parent_name_text',
        'area_name_text',
        'xiaoqu_text',
        'jsms_text',
        'bao_text',
        'image_text',
        'call_image_text',
        'order_id'
    ];

    public function getOrderIdAttr($value, $data)
    {
        if(isset($data['platform_type'])){
            if($data['platform_type'] == 7 && $data['outTradeNo']){
                $value = $data['outTradeNo'];
            }
        }
        return $value;
    }

    public function getImageTextAttr($value, $data)
    {
        $value = '';
        if (isset($data['image']) && isset($data['is_agency'])) {
            if ($data['is_agency'] == 2) {
                if(!empty($data['image'])){
                    $value = config('site.user_url').$data['image'];
                }

            }
        }
        return $value;
    }

    public function getCallImageTextAttr($value, $data)
    {
        if (isset($data['call_image']) && isset($data['is_agency'])) {
            if ($data['is_agency'] == 2) {
                if(!empty($data['call_image'])){
                    $value = config('site.user_url').$data['call_image'];
                }
            }
        }
        return $value;
    }

    public function getParentNameTextAttr($value, $data)
    {
        if (isset($data['parent_id'])) {
            $value = User::where(['id' => $data['parent_id']])->value('username');
        }
        return $value;
    }

    public function getAreaNameTextAttr($value, $data)
    {
        if (isset($data['shipping_area'])) {
            $value = \app\admin\model\user\Agency::where(['area' => $data['shipping_area']])->value('username');
        }
        return $value;
    }

    public function getXiaoquTextAttr($value, $data)
    {
        if (isset($data['shipping_address'])) {
            $value = \app\admin\model\user\Agency::where(['address' => $data['shipping_address']])->value('username');
        }
        return $value;
    }

    public function getJsmsTextAttr($value, $data)
    {
        if (isset($data['order_status']) && isset($data['is_more']) && isset($data['more_num'])) {
            if ($data['order_status'] == 5) {
                if ($data['is_more'] == 1 || $data['more_num'] > 1) {
                    $value = "已全部结算";
//                    $query->baoxiao_msg = "已全部报销";
                } else {
                    if ($data['is_more'] == 2 && $data['more_num'] == 1) {
                        $value = "次月待结算";
//                    $query->baoxiao_msg = "首月已报销";
                    }
                }
            }else{
                if($data['is_more'] == 1){
                    $value = "待结算";
                }else{
                    $value = "首月待结算";
                }
            }
        }
        return $value;
    }

    public function getBaoTextAttr($value, $data)
    {
        if (isset($data['order_status']) && isset($data['is_more']) && isset($data['more_num'])) {
            if ($data['order_status'] == 5) {
                if ($data['is_more'] == 1 || $data['more_num'] > 1) {
                    $value = "已全部报销";
                } else {
                    if ($data['is_more'] == 2 && $data['more_num'] == 1) {
                        $value= "次月待报销";
                    }
                }
            }else{
                if($data['is_more'] == 1){
                    $value = "待报销";
                }else{
                    $value = "首月待报销";
                }
            }
        }
        return $value;
    }


    public function getIsCallbackList()
    {
        return ['1' => __('Is_callback 1'), '2' => __('Is_callback 2')];
    }

    public function getJssbTypeList()
    {
        return ['1' => '失败处理中', '2' => '结算失败'];
    }

    public function getIsHexiaoList()
    {
        return ['1' => __('否'), '2' => __('是')];
    }

    public function getPlatformTypeList()
    {
        return ['1' => __('Platform_type 1'), '2' => __('Platform_type 2'),'3' => __('Platform_type 3'),'4' => __('Platform_type 4'),'5' => __('Platform_type 5'),'6' => __('Platform_type 6'),'7' => __('Platform_type 7'),'8' => __('Platform_type 8')];
    }

    public function getOrderStatusList()
    {
        return [
            '0' => __('Order_status 0'),
            '1' => __('Order_status 1'),
            '2' => __('Order_status 2'),
            '3' => __('Order_status 3'),
            '4' => __('Order_status 4'),
            '5' => __('Order_status 5'),
            '6' => __('Order_status 6')
        ];
    }

    public function getIsRechargedList()
    {
        return ['0' => __('Is_recharged 0'), '1' => __('Is_recharged 1')];
    }


    public function getIsCallbackTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_callback']) ? $data['is_callback'] : '');
        $list  = $this->getIsCallbackList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOrderStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $list  = $this->getOrderStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsRechargedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recharged']) ? $data['is_recharged'] : '');
        $list  = $this->getIsRechargedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCallbackTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['callback_time']) ? $data['callback_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCallbackTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function packages()
    {
        return $this->belongsTo('Packages', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
